package com.tebiecloud.mpweixin.server.utils;

import com.alibaba.fastjson.JSONObject;
import com.tebiecloud.common.utils.NetworkUtils;

/**
 * 公众号对应的接口，请参照 http://mp.weixin.qq.com/wiki/home/index.html
 * @author chenxiaoc
 *
 */
public class MPApi {

	private final static String GetToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
	// 消息管理
	private final static String PostMessageCustom = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s";
	// 用户管理
	private final static String GetUserGet = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s";
	private final static String GetUserInfo = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=%s";
	// 素材管理
	private final static String PostMediaUpload = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s";
	private final static String GetMediaGet = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s";
	private final static String PostMediaAddnews = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=%s";
//	private final static String PostMediaAddmaterial = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s&type=%s";
	private final static String PostMediaUpdatenews = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=%s";
	private final static String PostMediaDelmaterial = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=%s";
	// 账号管理
	private final static String PostQrcodeCreate = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s";
	private final static String PostShowqrcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s";
	// JS-SDK
	private final static String GetGetticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi";
	// 通过网页授权access_token获取用户基本信息（需授权作用域为snsapi_userinfo）
	private static final String GetSnsUserinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";

	/** token是否无效 */
	public static boolean isInvalueToken(int errcode) {
		if(errcode == 40001 || errcode == 40014 || errcode == 41001 || errcode == 42001) {
			return true;
		}
		return false;
	}
	
	/** 获取AccessToken */
	public static String getToken(String appid, String secret) {
		return NetworkUtils.doGet(String.format(GetToken, appid, secret));
	}
	
	/** 客服接口-发消息 */
	public static String postMessageCustom(String access_token, String data) {
		return NetworkUtils.doPost(String.format(PostMessageCustom, access_token), data);
	}
	
	/** 获取用户列表 */
	public static String userGet(String access_token, String next_openid) {
		return NetworkUtils.doGet(String.format(GetUserGet, access_token, next_openid != null ? next_openid : ""));
	}
	
	/** 获取用户基本信息（包括UnionID机制） */
	public static String userInfo(String access_token, String openid, String lang) {
		return NetworkUtils.doGet(String.format(GetUserInfo, access_token, openid != null ? openid : "", lang != null ? lang : ""));
	}
	
	/** 新增临时素材 */
	public static String mediaUpload(String access_token, String type, String filePath) {
		return NetworkUtils.upload(String.format(PostMediaUpload, access_token, type), filePath, null);
	}
	
	/** 获取临时素材 */
	public static String mediaGet(String access_token, String media_id, String path) {
		return NetworkUtils.download(String.format(GetMediaGet, access_token, media_id), path);
	}
	
	/** 新增永久图文素材 */
	public static String mediaAddnews(String access_token, String data) {
		return NetworkUtils.doPost(String.format(PostMediaAddnews, access_token), data);
	}
	
//	/** 新增其他类型永久素材 */
//	public static String mediaAddmaterial(String access_token, String type, String mediaurl) {
//		return NetworkUtils.uploadMedia(String.format(PostMediaAddmaterial, access_token, type), mediaurl);
//	}
	
	/** 修改永久图文素材 */
	public static String mediaUpdatenews(String access_token, String data) {
		return NetworkUtils.doPost(String.format(PostMediaUpdatenews, access_token), data);
	}
	
	/** 删除永久素材 */
	public static String mediaDelmaterial(String access_token, String data) {
		return NetworkUtils.doPost(String.format(PostMediaDelmaterial, access_token), data);
	}
	
	/** 创建二维码ticket */
	public static String qrcodeCreate(String access_token, String data) {
		return NetworkUtils.doPost(String.format(PostQrcodeCreate, access_token), data);
	}
	
	/** 通过ticket换取二维码 */
	public static String showqrcode(String ticket) {
		return NetworkUtils.doGet(String.format(PostShowqrcode, ticket));
	}
	
	/** 请求获得jsapi_ticket */
	public static String getticket(String access_token) {
		return NetworkUtils.doGet(String.format(GetGetticket, access_token));
	}

	/** 通过网页授权access_token获取用户基本信息 */
	public static String getSnsUserinfo(String access_token, String openid) {
		return NetworkUtils.doGet(String.format(GetSnsUserinfo, access_token, openid));
	}
	
}
